package com.sky.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 衷岚
 * @version 1.0
 */

/**
 * rabbitmq
 * 消费者 消费失败重试机制
 * retry失败后，把消息放入特定队列，等待人工处理
 */
@Configuration
@ConditionalOnProperty(value = "spring.rabbitmq.listener.simple.retry.enabled",havingValue = "true")
public class MqRetryConfiguration {

    @Bean
    public Queue errorQueue() {
        return new Queue("error.queue");
    }
    @Bean
    public DirectExchange errorExchange() {
        return new DirectExchange("error.direct");
    }

    @Bean
    public Binding errorBinding(Queue errorQueue, DirectExchange errorExchange) {
        return BindingBuilder.bind(errorQueue).to(errorExchange).with("error");
    }

    @Bean
    public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate) {
        return new RepublishMessageRecoverer(rabbitTemplate,"error.direct","error");
    }
}
